<?php
/*
Plugin Name: All Young
Description: 
Author: anti & v_serov
Version: 0.1
Author URI: http://vmrf.ru/
*/

require_once 'init.php';
require_once 'functions.php';

function loadAllYoung() {
    ay()->session();
    registerAllYoungHooks();
    
    //error_log(print_r(ay()->region(),true));
}

/**
* Активация плагина
* 
* @since 0.1
* @return void
*/
function activateAllYoung() {
    global $wpdb;
    $config = ay()->config();

    //Активация для плагина Region IP 
    $table_name = $wpdb->prefix . $config->regDbName;
    if ( $wpdb->get_var('SHOW TABLES LIKE ' . $table_name) != $table_name ) {
        $sql = 'CREATE TABLE ' .$table_name. ' (
                    `user_id` int(10) NOT NULL,
                    `reg_id` int(10) NOT NULL,
                    PRIMARY KEY (`user_id`)
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8';

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);

        add_option('regip_database_version', REGIP_DB);
    }
//Конец активации

    return true;
}
/**
* Функции для хуков
* 
* @since 0.1
* @return void
*/
function generateExcerptField($id){
    //in functions.php
    addExcerptField($id);
}
/**
* Регистрация всех хуков
* Обязательно все хуки должны активироваться здесь
* 
* @since 0.1
* @return void
*/
function registerAllYoungHooks() {
    add_action('widgets_init','registerAllYoungWidget');
    add_action('save_post', 'generateExcerptField');
     $contributor = get_role('contributor');
    $contributor->add_cap('upload_files');



    return true;
}

/**
* Регистрация всех widgets
* 
* 
* @since 0.1
* @return void
*/
function registerAllYoungWidget() {
    $widgetDirectory= AY_DIR_LIB . DIRECTORY_SEPARATOR . 'AY' . DIRECTORY_SEPARATOR . 'Widget';
    $directory = new DirectoryIterator($widgetDirectory);
    foreach ($directory as $widget) {
        if ($widget->isFile() && $widget->getFilename() != 'AWidget.php' && preg_match('/^(\w+)\.php$/', $widget->getFilename())) {
            $widgetName = str_replace('.php', '', $widget->getFilename());
            $className = '\\AY\\Widget\\' . $widgetName;
            register_widget($className);
        }
    }
}
// Now we set that function up to execute when the plugins_loaded action is called
add_action( 'plugins_loaded', 'loadAllYoung' );
register_activation_hook(__FILE__, 'activateAllYoung');

?>
